package root.electronclass.electronclass.mvp.rest;


import root.electronclass.electronclass.mvp.base.gson.GsonConverterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class RestManager {
    public static volatile RestManager sApiManager = null;
    public static volatile RestApi mApiService;
    public static volatile ClassApi mClassApiService;
    public static final String url = "http://36.134.62.252:12313/";//测试环境
//    public static final String url = "https://web.zf.zjbenti.com/dzbpApi/v4/";//测试环境
//    public static final String url = "http://api.dzbp.ztengit.com/";//正式环境
    public String TOKEN = "";

    public static Gson gson = new GsonBuilder().setLenient().create();

    public static OkHttpClient getHttpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(60000, TimeUnit.MILLISECONDS)
                .readTimeout(60000, TimeUnit.MILLISECONDS)
                .addInterceptor(new VoidInterceptor())
                .addInterceptor(chain -> {
                    Request original = chain.request();
                    Request request = original.newBuilder()
                            .addHeader("Authorization", getInstance().getTOKEN())
                            .method(original.method(), original.body())
                            .build();
                    return chain.proceed(request);

                })
                .build();
    }
    public String getTOKEN() {
        return TOKEN;
    }

    public void setTOKEN(String TOKEN) {
        this.TOKEN = TOKEN;
    }

    public static RestManager getInstance() {
        if (sApiManager == null) {
            synchronized (RestManager.class) {
                if (sApiManager == null) {
                    sApiManager = new RestManager();
                }
            }
        }
        return sApiManager;
    }

    public void initService(){
        mApiService = null;
        mClassApiService = null;
    }

    public RestApi initRest() {
        if (mApiService == null) {
            synchronized (RestManager.class) {
                if (mApiService == null) {

                    GsonBuilder builder = new GsonBuilder();
                    builder.registerTypeAdapter(Date.class,
                            (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
                    mApiService = new Retrofit.Builder()
                            .baseUrl(url)
                            .addConverterFactory(GsonConverterFactory.create(gson))
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .client(getHttpClient())
                            .build()
                            .create(RestApi.class);
                }
            }
        }
        return mApiService;
    }
    public ClassApi classRest() {
        if (mClassApiService == null) {
            synchronized (RestManager.class) {
                if (mClassApiService == null) {
                    mClassApiService = new Retrofit.Builder()
                            .baseUrl(url)
                            .addConverterFactory(GsonConverterFactory.create(gson))
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .client(getHttpClient())
                            .build()
                            .create(ClassApi.class);
                }
            }
        }
        return mClassApiService;
    }
}
